import * as TYPE from "../action-type";
import { sStorage } from "@/utils/storage";
import store from "..";

//白名单
const WITHOUT_TAG_PATHS = ["/404", "/login"];

const TagsAction = {
  setActiveTag(path) {
    sStorage.set("activeTag", path);
    store.dispatch({
      type: TYPE.TAGS_SETACTIVETAG,
      activeTag: path,
    });
  },
  setTags(tags) {
    sStorage.set("tags", tags);
    store.dispatch({
      type: TYPE.TAGS_SETTAGS,
      tags,
    });
  },
  /**
   * tag包含name, path, title, icon
   * @param {*} tag
   */
  addTag(tag = {}, tags = []) {
    this.setActiveTag(tag.path); //设置当前激活项
    //如果出现在白名单或者已经在列表中泽不添加
    if (
      WITHOUT_TAG_PATHS.includes(tag.path) ||
      tags.some((item) => item.path === tag.path)
    ) {
      return {
        type: TYPE.TAGS_ADDTAGS,
      };
    }

    let Store = store.getState();
    this.setTags([...Store.tags.tags, tag]);

    return {
      type: TYPE.TAGS_ADDTAGS,
    };
  },
  removeTag(path) {
    //删除点击的标签
    let Store = store.getState();
    //过滤
    this.setTags(Store.tags.tags.filter((tag) => tag.path !== path));
  },
  removeOther() {},
  removeLeft(path) {},
  removeRight(path) {},
  resetTags() {
    this.setTags([]);
    this.setActiveTag("");
    return {
      type: TYPE.TAGS_RESETTAGS,
    };
  },
};

export default TagsAction;
